|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
BexeeProperties.java | 75% | 90% | 100% | 89.7% |
|
1 |
/*
|
|
2 |
* $Id: BexeeProperties.java,v 1.1 2004/12/15 14:18:20 patforna Exp $
|
|
3 |
*
|
|
4 |
* Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
|
|
5 |
* Berne University of Applied Sciences
|
|
6 |
* School of Engineering and Information Technology
|
|
7 |
* All rights reserved.
|
|
8 |
*/
|
|
9 |
package bexee.util;
|
|
10 |
|
|
11 |
import java.io.IOException;
|
|
12 |
import java.io.InputStream;
|
|
13 |
import java.util.Properties;
|
|
14 |
|
|
15 |
import org.apache.commons.logging.Log;
|
|
16 |
import org.apache.commons.logging.LogFactory;
|
|
17 |
|
|
18 |
/**
|
|
19 |
* Provides a convenient way for accessing system properties and bexee
|
|
20 |
* properties of which the latter are stored in a file called
|
|
21 |
* <code>bexee.properties</code> and must be somewhere in the classpath.
|
|
22 |
* <p>
|
|
23 |
* Note that system properties take precedence over all other properties unless
|
|
24 |
* a property is explicitly programmaticly set.
|
|
25 |
* <p>
|
|
26 |
* The system properties are always checked for changes whereas the properties
|
|
27 |
* from the <code>bexee.properties</code> are only read once upon creation.
|
|
28 |
* <p>
|
|
29 |
* The class provides a number of static methods that always check first if the
|
|
30 |
* properties had been loaded from the properties file.
|
|
31 |
*
|
|
32 |
* @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:20 $
|
|
33 |
* @author Patric Fornasier
|
|
34 |
*/
|
|
35 |
public class BexeeProperties { |
|
36 |
|
|
37 |
protected static final String FILE_NAME = "bexee.properties"; |
|
38 |
|
|
39 |
private static Log log = LogFactory.getLog(BexeeProperties.class); |
|
40 |
|
|
41 |
// union of bexee and system properties
|
|
42 |
protected static Properties props; |
|
43 |
|
|
44 |
// properties from bexee.properties file
|
|
45 |
protected static Properties bexee; |
|
46 |
|
|
47 |
/**
|
|
48 |
* Searches for the property with the specified key in this property list.
|
|
49 |
* The method returns null if the property is not found.
|
|
50 |
*
|
|
51 |
* @param key
|
|
52 |
* the property key.
|
|
53 |
* @return the value in this property list with the specified key value.
|
|
54 |
*/
|
|
55 | 110 |
public static String getProperty(String key) { |
56 | 110 |
init(); |
57 | 110 |
return props.getProperty(key);
|
58 |
} |
|
59 |
|
|
60 |
/**
|
|
61 |
* Searches for the property with the specified key in this property list.
|
|
62 |
* The method returns the default value argument if the property is not
|
|
63 |
* found.
|
|
64 |
*
|
|
65 |
* @param key
|
|
66 |
* the hashtable key
|
|
67 |
* @param defaultValue
|
|
68 |
* a default value
|
|
69 |
* @return the value in this property list with the specified key value
|
|
70 |
*/
|
|
71 | 6 |
public static String getProperty(String key, String defaultValue) { |
72 | 6 |
init(); |
73 | 6 |
return props.getProperty(key, defaultValue);
|
74 |
} |
|
75 |
|
|
76 |
/**
|
|
77 |
* Returns the initialized underlying property file.
|
|
78 |
*
|
|
79 |
* @return a <code>Property</code> object.
|
|
80 |
*/
|
|
81 | 2 |
public static Properties getProperties() { |
82 | 2 |
init(); |
83 | 2 |
return props;
|
84 |
} |
|
85 |
|
|
86 |
/**
|
|
87 |
* Calls the Hashtable method put. Provided for parallelism with the
|
|
88 |
* getProperty method. Enforces use of strings for property keys and values.
|
|
89 |
* The value returned is the result of the Hashtable call to put.
|
|
90 |
*
|
|
91 |
* @param key
|
|
92 |
* the key to be placed into this property list
|
|
93 |
* @param value
|
|
94 |
* the value corresponding to key
|
|
95 |
* @return the previous value of the specified key in this property list, or
|
|
96 |
* null if it did not have one
|
|
97 |
*/
|
|
98 | 2 |
public static Object setProperty(String key, String value) { |
99 | 2 |
init(); |
100 | 2 |
return props.setProperty(key, value);
|
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* Makes sure the properties are initialized. This method is the first thing
|
|
105 |
* called in every method inside this class.
|
|
106 |
* <p>
|
|
107 |
* If created for the first time this method tries to load the properties
|
|
108 |
* from a property file and then adds system properties, which means that
|
|
109 |
* application specified properties may be overriden.
|
|
110 |
*/
|
|
111 | 120 |
private static void init() { |
112 | 120 |
if (props == null) { |
113 |
|
|
114 | 24 |
props = new Properties();
|
115 | 24 |
bexee = new Properties();
|
116 |
|
|
117 |
// find properties file
|
|
118 | 24 |
ClassLoader loader = BexeeProperties.class.getClassLoader();
|
119 | 24 |
InputStream is = loader.getResourceAsStream(FILE_NAME); |
120 |
|
|
121 |
// try to load it
|
|
122 | 24 |
if (is == null) { |
123 | 0 |
log.info("unable to locate file: " + FILE_NAME);
|
124 |
} else {
|
|
125 | 24 |
try {
|
126 |
// load properties from file once and store them in bexee
|
|
127 | 24 |
bexee.load(is); |
128 |
// copy properties (they can be overriden by system props)
|
|
129 | 24 |
props.putAll(bexee); |
130 |
} catch (IOException e) {
|
|
131 | 0 |
log.info("unable to load file: " + FILE_NAME, e);
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
// always override with latest system properties
|
|
137 | 120 |
props.putAll(System.getProperties()); |
138 |
} |
|
139 |
} |
|